package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Secondary;
import cn.wolfcode.crm.mapper.ProductFirstMapper;
import cn.wolfcode.crm.mapper.SecondaryMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.ProductQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISecondaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class SecondaryServiceImpl implements ISecondaryService {
    @Autowired
    private SecondaryMapper secondaryMapper;
    @Autowired
    private ProductFirstMapper productFirstMapper;

    public Secondary selectByPrimaryKey(Long id) {
        return secondaryMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<Secondary> selectByFirstId(Long firstId) {
        return secondaryMapper.selectByFirstId(firstId);
    }

    public void delete(Long id) {
        secondaryMapper.deleteByPrimaryKey(id);
    }
    //页面信息对象,根据easyUI的DataGrid组件,返回的数据为json对象,且只有total,rows两个属性
    public PageResult query(QueryObject qo) {
        int count = secondaryMapper.selectCount(qo);
        if (count==0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Secondary> rows = secondaryMapper.queryForList(qo);
        return new PageResult(count,rows);
    }

    public void saveOrUpdate(Secondary secondary) {
        if (secondary.getId() == null) {

            secondaryMapper.insert(secondary);
        }else{
            secondaryMapper.updateByPrimaryKey(secondary);
        }
    }

    public List<Secondary> selectAll(ProductQueryObject qo) {
        return secondaryMapper.selectAll(qo);
    }


}
